﻿using System;
using System.Collections.Generic;
using Opalis.QuickIntegrationKit;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using System.Text;
using System.Net;

namespace ExchangeMail
{
    [OpalisObject("Delete Folder", ShowFilters = false)]
    public class DeleteFolder : IOpalisObject
    {
        private MailboxSettings settings;
        private String exchangeVersion = String.Empty;
        
        private String FolderID = String.Empty;
        private String FolderName = String.Empty;

        private String userName = String.Empty;
        private String password = String.Empty;
        private String domain = String.Empty;
        private String serviceURL = String.Empty;

        [OpalisConfiguration]
        public MailboxSettings Settings
        {
            get { return settings; }
            set { settings = value; }
        }


        public void Design(IOpalisDesigner designer)
        {
            designer.AddInput("Folder Name");
            designer.AddInput("Folder ID").NotRequired();
            designer.AddOutput("Folder Name").AsString();
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            userName = settings.UserName;
            password = settings.Password;
            domain = settings.Domain;
            serviceURL = settings.ServiceUrl;
            exchangeVersion = settings.ExchangeVersion;

            FolderName = request.Inputs["Folder Name"].AsString();
            FolderID = request.Inputs["Folder ID"].AsString();

           ExchangeService service = new ExchangeService();
            switch (exchangeVersion)
            {
                case "Exchange2007_SP1":
                    service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                    break;
                case "Exchange2010":
                    service = new ExchangeService(ExchangeVersion.Exchange2010);
                    break;
                case "Exchange2010_SP1":
                    service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
                    break;
                default:
                    service = new ExchangeService();
                    break;
            }

            service.Credentials = new WebCredentials(userName, password, domain);
            String AccountUrl = userName + "@" + domain;

            if (serviceURL.Equals("Autodiscover")) { service.AutodiscoverUrl(AccountUrl); }
            else { service.Url = new Uri(serviceURL); }

            FolderId fID = new FolderId(WellKnownFolderName.Inbox);

            if (FolderID != String.Empty)
            {
                fID =new FolderId(FolderID);

                SearchFilter folderFilter = new SearchFilter.IsEqualTo(FolderSchema.Id, FolderID);
                FolderView folderView = new FolderView(int.MaxValue);
                folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
                folderView.PropertySet.Add(FolderSchema.DisplayName);
                folderView.Traversal = FolderTraversal.Deep;
                FindFoldersResults results = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderFilter, folderView);

                foreach (Folder tempFolder in results)
                {
                    tempFolder.Delete(DeleteMode.HardDelete);
                }

            }    
            else
            {
                SearchFilter folderFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, FolderName);
                FolderView folderView = new FolderView(int.MaxValue);
                folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
                folderView.PropertySet.Add(FolderSchema.DisplayName);
                folderView.Traversal = FolderTraversal.Deep;
                FindFoldersResults results = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderFilter, folderView);

                foreach (Folder tempFolder in results)
                {
                    tempFolder.Delete(DeleteMode.HardDelete);
                }
            }

            response.Publish("Folder Name", FolderName);
        }
    }
}
